package hl.demosample.ui.adapter

import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import hl.demosample.R
import hl.demosample.app.ext.setAdapterAnimation
import hl.demosample.app.util.ColorUtil
import hl.demosample.app.util.SettingUtil
import hl.demosample.app.weight.customview.CollectView
import hl.demosample.data.model.bean.HomeData

/*
* path: DemoSample 
* Author: 86133
* Create: 2021-06-02 10:59
* Changes (from 2021-06-02)
* Desc:
*/
class HomeAdapter(data: MutableList<HomeData>?) :
    BaseQuickAdapter<HomeData, BaseViewHolder>(R.layout.home_item, data) {

    private var showTag = false

    private var collectAction: (item: HomeData, v: CollectView, position: Int) -> Unit =
        { _: HomeData, _: CollectView, _: Int -> }

    constructor(data: MutableList<HomeData>?, showTag: Boolean) : this(data) {
        this.showTag = showTag
    }

    init {
        setAdapterAnimation(SettingUtil.getListMode())
    }

    override fun convert(holder: BaseViewHolder, item: HomeData) {
        item.run {
            Glide.with(context).load(imageUrl)
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .apply(RequestOptions().placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher))
                .into(holder.getView(R.id.item_project_imageview))
            holder.setText(R.id.item_project_new,label)
            holder.setText(R.id.item_project_title,title)
            holder.setText(R.id.item_project_date,data)
            holder.getView<CollectView>(R.id.item_home_collect).isChecked = collect
        }
        holder.getView<CollectView>(R.id.item_home_collect)
            .setOnCollectViewClickListener(object : CollectView.OnCollectViewClickListener {
                override fun onClick(v: CollectView) {
                    collectAction.invoke(item, v, holder.adapterPosition)
                }
            })
    }

    fun setCollectClick(inputCollectAction: (item: HomeData, v: CollectView, position: Int) -> Unit) {
        this.collectAction = inputCollectAction
    }

}